package com.cxl.bill.server.service;

import com.cxl.bill.server.db.bean.Bill;
import com.cxl.bill.server.db.bean.User;
import com.cxl.bill.server.db.dao.BillDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class BillService {
    private final BillDao billDao;

    public BillService(BillDao billDao) {
        this.billDao = billDao;
    }


    /**
     * 新增
     * @param userId 用户Id
     * @param amount 金额
     * @param explan 说明
     * @param remark 备注
     * @return 成功 true
     */
    public boolean insert(Long userId, Double amount, String explan, String remark) {
        return billDao.insert(
                Bill
                        .builder()
                        .userId(userId)
                        .amount(amount)
                        .explan(explan)
                        .remark(remark)
                        .build()
        ) > 0;
    }

    /**
     * 更新
     * @param id 账单id
     * @param amount 金额
     * @param explan 说明
     * @param remark 备注
     * @return 成功 true
     */
    public boolean update(Long id,Double amount,String explan, String remark){
        return billDao.updateByPrimaryKeySelective(
                Bill
                        .builder()
                        .id(id)
                        .amount(amount)
                        .explan(explan)
                        .remark(remark)
                        .uTime(new Date())
                        .build()
        )>0;
    }

    /**
     * 根据账单Id查询账单详情
     * @param id id
     * @return Bill
     */
    public Bill findById(Long id){
        return billDao.selectByPrimaryKey(id);
    }



    /**
     * 根据用户Id查询账单列表
     * @param userId 用户Id
     * @return 账单列表
     */
    public Page<Bill> listByUserId(Long userId){
        return billDao.listByUserId(userId);
    }
}
